﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Constants;

namespace HHPS.CreditEducation.Presentation.StudyPlans
{
    public partial class ElementControl : UserControl
    {
        //public ElementControl()
        //{
        //    InitializeComponent();
        //}

        private readonly StudyPlan _studyPlan;
        private readonly Business.Business _business;

        public ElementControl(string studyPlanName)
        {
            InitializeComponent();

            _business = MainView.Instance.CreditEducationBusiness;
            lblTitle.Text = studyPlanName;

            _studyPlan = _business.DataContex.StudyPlans.First(s => s.Name == studyPlanName);

            if (_studyPlan == null)
            {
                MessageBox.Show("Không thể lấy dữ liệu kế hoạch " + studyPlanName);
            }
            else
            {
                lblMajor.Text = _studyPlan.Major.Name;
                lblStartDate.Text = _studyPlan.StartDate.ToShortDateString();
                lblDetail.Text = _studyPlan.Detail;
            }
        }

        private void ElementControl_Load(object sender, EventArgs e)
        {
            Dock = DockStyle.Top;
            LoadCourseData();
        }

        private void LoadCourseData()
        {
            foreach (var studyCourse in _business.GetStudyCourseInStudyPlan(_studyPlan.Id))
            {
                pnlCourses.Controls.Add(new CourseElementControl(studyCourse));
                Height = 180 + pnlCourses.Controls.Count * pnlCourses.Controls[0].Height;
            }
        }

        private void AddNewCourse()
        {
            if (!_business.AllStudyCourseIsComplete(_studyPlan))
            {
                MessageBox.Show("Bạn cần phải hoàn thành hết các kỳ trước khi thêm kỳ mới");
                return;
            }

            var course = _business.GetCourseNotExistInStudyPlan(_studyPlan.Id);

            if (course != null)
            {
                var studyCourse = new StudyCourse
                                      {
                                          Course = course,
                                          StudyPlan = _studyPlan,
                                          IsComplete = false,
                                          StartDate = DateTime.Now,
                                      };

                pnlCourses.Controls.Add(new CourseElementControl(studyCourse));
                Height = 180 + pnlCourses.Controls.Count * pnlCourses.Controls[0].Height;

                //Save new course to database
                _business.AddNewStudyCourse(studyCourse);
            }
            else
            {
                MessageBox.Show("Bạn đã có kế hoạch của tất cả các kì học");
            }
        }

        private void btnAddFaculty_Click(object sender, EventArgs e)
        {
            AddNewCourse();
        }

        private void btnDeleteCourse_Click(object sender, EventArgs e)
        {
            var studyCourses = pnlCourses.Controls.Cast<object>().Select(control => ((CourseElementControl) control).StudyCourse).Where(studyCourse => studyCourse.IsChecked).ToList();
            //Save change to database
            if (studyCourses.Count > 0)
            {
                if (MessageBox.Show(string.Format(ConstantMessages.ConfirmDeleteItem, " các kỳ học đã chọn"), FileConstants.PROGRAM_NAME, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    _business.DeleteStudyCourses(studyCourses);
                    for (int i = pnlCourses.Controls.Count - 1; i >= 0; i--)
                    {
                        var studyCourse = ((CourseElementControl)pnlCourses.Controls[i]).StudyCourse;
                        if (studyCourse.IsChecked)
                        {
                            pnlCourses.Controls.Remove(pnlCourses.Controls[i]);
                        }
                    }
                }
            }
        }
    }
}
